import router from './router'
import store from './store'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'

const whileList = ['/login', '/404']
// 前置守卫
router.beforeEach((to, from, next) => {
  nprogress.start()
  const { token } = store.getters
  if (token) {
    if (to.path === '/login') {
      next('/')
      nprogress.done()
    } else {
      next()
    }
  } else {
    if (whileList.includes(to.path)) {
      next()
    } else {
      next('/login')
      nprogress.done()
    }
  }
})

router.afterEach(() => {
  nprogress.done()
})
